<template>
  <el-dialog v-model="dialogVisible" :destroy-on-close="true" width="750px" :title="`${dialogVProps.title}`">
    <el-form
      ref="ruleFormRef"
      label-width="160px"
      label-suffix=" :"
      :rules="rules"
      :disabled="dialogVProps.isView"
      :model="dialogVProps.row"
      :hide-required-asterisk="dialogVProps.isView"
    >
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="用户名" prop="userName">
            <el-input v-model="dialogVProps.row!.userName" disabled />
          </el-form-item>
          <el-form-item label="上级商铺" prop="parentStoreId">
            <el-input v-model="dialogVProps.row!.parentStoreName" disabled>
              <template #append> <el-button @click="openSelectTable('商户选择', 0)" :icon="MoreFilled" /> </template>
            </el-input>
          </el-form-item>
          <el-form-item label="门店名称" prop="storeName">
            <el-input v-model="dialogVProps.row!.storeName" clearable></el-input>
          </el-form-item>
          <el-form-item label="门店电话" prop="mobile">
            <el-input v-model="dialogVProps.row!.mobile" clearable></el-input>
          </el-form-item>
          <el-form-item label="所属行业" prop="industryId">
            <!-- <el-select class="w100" v-model="dialogVProps.row!.industryId" placeholder="请选择行业">
              <el-option v-for="item in industryList" :key="item.id" :label="item.name" :value="item.id" />
            </el-select> -->
            <el-cascader
              check-strictly
              :props="{ label: 'name', value: 'id', emitPath: false }"
              v-model="dialogVProps.row!.industryId"
              :options="industryList"
            />
          </el-form-item>
          <el-form-item v-if="dialogVProps.isService" label="小程序AppID" prop="appId">
            <el-input v-model="dialogVProps.row!.appId" clearable />
          </el-form-item>
          <el-form-item v-if="dialogVProps.isService" label="有效期" prop="endDate">
            <el-date-picker class="w100" v-model="dialogVProps.row!.endDate" type="date" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="支付账号" prop="payAccount">
            <el-input v-model="dialogVProps.row!.payAccount" clearable></el-input>
          </el-form-item>
          <el-form-item label="所属区域" prop="areaObject">
            <el-cascader class="w100" v-model="dialogVProps.row!.areaObject" :options="AreaList" :props="CasProps"></el-cascader>
          </el-form-item>
          <el-form-item label="门店地址" prop="address">
            <el-input v-model="dialogVProps.row!.address" clearable></el-input>
          </el-form-item>
          <el-form-item label="门店联系人" prop="linkMan">
            <el-input v-model="dialogVProps.row!.linkMan" clearable></el-input>
          </el-form-item>
          <el-form-item v-if="dialogVProps.isService" label="Redis订阅频道号" prop="channel">
            <el-input v-model="dialogVProps.row!.channel" />
          </el-form-item>
          <el-form-item v-if="dialogVProps.isService" label="小程序appSecret" prop="appSecret">
            <el-input v-model="dialogVProps.row!.appSecret" />
          </el-form-item>

          <el-form-item label="第三方平台URL" prop="syncUrl">
            <el-input v-model="dialogVProps.row!.syncUrl" />
          </el-form-item>
          <el-form-item label="是否同步" prop="syncFlag">
            <el-radio-group v-model="dialogVProps.row!.syncFlag">
              <el-radio :value="true" label="是" />
              <el-radio :value="false" label="否" />
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <SelectTable @radio="onRadio" ref="selectTableRef" />
    <template #footer>
      <el-button @click="dialogVisible = false"> 取消 </el-button>
      <el-button v-show="!dialogVProps.isView" type="primary" @click="handleSubmit"> 确定 </el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="storeDrawer">
import { ref, reactive } from "vue";
import { ElMessage, FormInstance } from "element-plus";
import { Login, Operation } from "@/api/interface";
import AreaList from "@/assets/json/pcaCode.json";
import SelectTable from "@/components/SelectTable/index.vue";
import { getStoreList } from "@/api/modules/operation";
import { MoreFilled } from "@element-plus/icons-vue";
import { getIndustryList, getStoreTypeListAPI } from "@/api/modules/login";

const rules = reactive({
  storeName: [{ required: true, message: "门店名称必填" }]
});

interface DialogVProps {
  title: string;
  isView: boolean;
  isService?: boolean;
  // row: Partial<Operation.ResDepartmentList>;
  row: any;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
}

const dialogVisible = ref(false);
const dialogVProps = ref<DialogVProps>({
  isView: false,
  title: "",
  row: {
    syncFlag: false
  }
});
const industryList = ref<Login.StoreTypeList[]>([]);
const CasProps = ref({ label: "name", value: "code" });
// 接收父组件传过来的参数
const acceptParams = async (params: DialogVProps) => {
  dialogVProps.value = params;
  const [industry] = await Promise.all([getIndustryList({ pageNum: 1, pageSize: 9999 }), getStoreTypeList()]);
  // industryList.value = industry.data;
  dialogVisible.value = true;
};

const getStoreTypeList = async () => {
  const res = await getStoreTypeListAPI({});
  industryList.value = res.data;
};

const onRadio = (row: any) => {
  dialogVProps.value.row.parentStoreId = row.data.id;
  dialogVProps.value.row.parentStoreName = row.data.storeName;
};

const selectTableRef = ref<InstanceType<typeof SelectTable> | null>(null);
const openSelectTable = (title: string, ix: number) => {
  const params = {
    title,
    isView: false,
    index: ix,
    initParam: { isGroup: 1 },
    selType: "radio",
    columns: [
      { type: "radio", label: "单选", width: 60 },
      { prop: "id", label: "编码", align: "left" },
      { prop: "storeName", label: "名称", align: "left" }
    ],
    api: getStoreList
  };
  selectTableRef.value?.acceptParams(params);
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      await dialogVProps.value.api!(dialogVProps.value.row);
      ElMessage.success({ message: `${dialogVProps.value.title}门店成功！` });
      dialogVProps.value.getTableList!();
      dialogVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  acceptParams
});
</script>
